import React, { useEffect, useRef } from "react";
import * as echarts from "echarts";

export default function Chart() {
  const chartsRef = useRef();

  const chartInit = () =>{
    var myChart = echarts.init(chartsRef.current);
    var option;
    option = {
      legend: {
        top: 'bottom'
      },
      toolbox: {
        show: true,
        feature: {
          mark: { show: true },
          dataView: { show: true, readOnly: false },
          restore: { show: true },
          saveAsImage: { show: true }
        }
      },
      series: [
        {
          name: 'Nightingale Chart',
          type: 'pie',
          radius: [50, 250],
          center: ['50%', '50%'],
          roseType: 'area',
          itemStyle: {
            borderRadius: 8
          },
          data: [
            { value: 40, name: 'vue2' },
            { value: 38, name: 'react' },
            { value: 32, name: 'vue3' },
            { value: 30, name: 'angular' },
            { value: 28, name: 'javascript' },
            { value: 26, name: 'typescript' },
            { value: 22, name: 'three.js' },
          ]
        }
      ]
    };
    option && myChart.setOption(option);
  }
  useEffect(() =>{
    chartInit()
  }, [])

  return (
    <div>
      {/* 准备一个挂载节点 */}
      <div ref={chartsRef} style={{width: "500px", height: "400px"}}></div>
    </div>
  )
}
